package com.uphone.magicleafteacher.pro.activity.mine;

import android.annotation.SuppressLint;
import android.arch.lifecycle.LifecycleOwner;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.CommonUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class MinefeedActivity extends BaseGActivity {
    @BindView(R.id.img_return)
    ImageView imgReturn;
    @BindView(R.id.tool_bar)
    Toolbar toolBar;
    @BindView(R.id.help_feedback)
    EditText helpFeedback;
    @BindView(R.id.but_help_feedback)
    Button butHelpFeedback;
    @BindView(R.id.max_num)
    TextView maxNum;
    private boolean isEmpty = true;

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_minefeed;
    }

    @Override
    protected void initView() {
        maxNum.setText(0 + "/100");
        helpFeedback.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                maxNum.setText(editable.toString().length() + "/100");
                if (TextUtils.isEmpty(editable.toString())) {
                    if (!isEmpty) {
                        butHelpFeedback.setBackgroundResource(R.drawable.shape_rectangle_round_bg_grayc_22);
                        isEmpty = true;
                    }
                } else {
                    if (isEmpty) {
                        butHelpFeedback.setBackgroundResource(R.drawable.shape_rectangle_round_bg_theme_22);
                        isEmpty = false;
                    }
                }
            }
        });
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.img_return, R.id.but_help_feedback})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_return:
                finish();
                break;
            case R.id.but_help_feedback:
                Submitopinions();
                break;
        }
    }

    @SuppressLint("CheckResult")
    private void Submitopinions() {
        if (CommonUtil.checkViewEmpty("请输入意见", helpFeedback))
            return;
        Map<String, Object> map1 = new HashMap<>();
        map1.put("teacherId", CommonUtil.getTeacherId());
        map1.put("suggestion", helpFeedback.getText().toString());
        MyApp.apiService(ApiService.class)
                .opinion(map1)
                .compose(RxSchedulers.io_main())
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    showLoading();
                })
                .doFinally(() -> {
                    closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        CommonUtil.quitActivity(MinefeedActivity.this, "意见提交成功");
                    }
                }, new FailureConsumer());
    }
}
